package com.maxrave.simpmusic.service.test.download

import android.app.Notification
import android.content.Context
import androidx.media3.common.util.NotificationUtil
import androidx.media3.common.util.UnstableApi
import androidx.media3.common.util.Util
import androidx.media3.exoplayer.offline.Download
import androidx.media3.exoplayer.offline.DownloadManager
import androidx.media3.exoplayer.offline.DownloadNotificationHelper
import androidx.media3.exoplayer.offline.DownloadService
import androidx.media3.exoplayer.scheduler.PlatformScheduler
import androidx.media3.exoplayer.scheduler.Scheduler
import com.maxrave.simpmusic.R
import com.maxrave.simpmusic.common.MEDIA_NOTIFICATION.NOTIFICATION_ID
import org.koin.android.ext.android.inject

@UnstableApi
class MusicDownloadService : DownloadService(
    NOTIFICATION_ID,
    1000L,
    CHANNEL_ID,
    R.string.download,
    0
) {
    /**
     * 这一行使用了 Koin，一个依赖注入框架，来获取 DownloadUtils 的实例。downloadUtil 是一个私有的只读属性。
     * by inject() 表示请求 Koin 提供 DownloadUtils 类的实例，这意味着在这个服务中会使用到 DownloadUtils 的方法和属性。
     * */
    private val downloadUtil: DownloadUtils by inject()

    override fun getDownloadManager() = downloadUtil.downloadManager

    override fun getScheduler(): Scheduler = PlatformScheduler(this, JOB_ID)

    override fun getForegroundNotification(downloads: MutableList<Download>, notMetRequirements: Int): Notification =
        downloadUtil.downloadNotificationHelper.buildProgressNotification(
            this,
            R.drawable.mono,
            null,
            if (downloads.size == 1) Util.fromUtf8Bytes(downloads[0].request.data)
            else resources.getQuantityString(R.plurals.n_song, downloads.size, downloads.size),
            downloads,
            notMetRequirements
        )




    class TerminalStateNotificationHelper(
        private val context: Context,
        private val notificationHelper: DownloadNotificationHelper,
        private var nextNotificationId: Int,
    ) : DownloadManager.Listener {

        override fun onDownloadChanged(
            downloadManager: DownloadManager,
            download: Download,
            finalException: Exception?,
        ) {
            if (download.state == Download.STATE_FAILED) {
                val notification = notificationHelper.buildDownloadFailedNotification(
                    context,
                    R.drawable.baseline_error_outline_24,
                    null,
                    Util.fromUtf8Bytes(download.request.data)
                )
                NotificationUtil.setNotification(context, nextNotificationId++, notification)
            }
            else if (download.state == Download.STATE_COMPLETED) {
                val notification = notificationHelper.buildDownloadCompletedNotification(
                    context,
                    R.drawable.baseline_downloaded,
                    null,
                    Util.fromUtf8Bytes(download.request.data)
                )
                NotificationUtil.setNotification(context, nextNotificationId++, notification)
            }
        }

        override fun onDownloadsPausedChanged(
            downloadManager: DownloadManager,
            downloadsPaused: Boolean
        ) {
            if (downloadsPaused) {
                downloadManager.resumeDownloads()
            }
        }
    }

    companion object {
        const val CHANNEL_ID = "download"  // 编译时常量
        const  val NOTIFICATION_TAG = "download_notification"
        const val NOTIFICATION_ID = 1000
        const val JOB_ID = 1000
    }
}